//
//  ParameterManager.m
//  Showing
//
//  Created by Ralf on 3/24/15.
//  Copyright (c) 2015 next-software. All rights reserved.
//

#import "ParameterManager.h"

@implementation ParameterManager

@synthesize server;
@synthesize parameters;
static NSString *deviceToken = nil;

+(void)SetDeviceToken:(NSString*)token{
     deviceToken = token;
}

+(NSString*)GetDeviceToken{
    if(deviceToken == nil){
        return @"";
    }
    else{
        return deviceToken;
    }
}

- (void)load
{
    NSString *sParametersFilePath = [self filePath];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:sParametersFilePath];
    if (fileExists)
    {
        self.parameters = [[NSMutableDictionary alloc] initWithContentsOfFile:sParametersFilePath];
        self.sessionID  = self.parameters[@"sessionID"];
    }
    else
    {
        self.parameters = [NSMutableDictionary new];
    }
}

- (void)save
{
    NSString *sParametersFilePath = [self filePath];

    if (self.sessionID && self.sessionID.length > 0)
    {
        self.parameters[@"sessionID"] = self.sessionID;
    }
    else
    {
        [self.parameters removeObjectForKey:@"sessionID"];
    }

    [self.parameters writeToFile:sParametersFilePath atomically:YES];
}

- (NSString *)filePath
{
    NSArray  *paths               = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sDocumentsDirectory = paths[0];
    NSString *sParametersFilePath = [sDocumentsDirectory stringByAppendingPathComponent:@"parameters.plist"];
    return sParametersFilePath;
}

- (instancetype)init
{
    if (self = [super init])
    {

        //        server = @"http://121.42.153.59:8080/star"; //product
        //        server = @"http://192.168.8.51:8080/star";
//                server = @"http://localhost:8080/star";
        //        server = @"http://114.215.136.123:8080/star";
                        server = @"http://115.28.56.226:8080/star";
//        server = @"http://192.168.8.140:8080/star";

        [self load];
    }

    return self;
}

@end
